package demo;

import demo.certificate.CerLoad;
import demo.certificate.PKCS12LoadImp;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
import org.eclipse.milo.opcua.stack.client.UaTcpStackClient;
import org.eclipse.milo.opcua.stack.core.types.builtin.*;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;

import java.io.File;
import java.security.KeyPair;
import java.security.cert.X509Certificate;
import java.util.concurrent.ExecutionException;

import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;

public class Demo {

    //定义OPC UA服务器的IP地址和端口 并且后面一定要跟 applicationName 不跟不行
    private static String EndPointUrl = "opc.tcp://DESKTOP-5CTK5AA:53530/OPCUA/SimulationServer";

    public static void main(String args[]) throws ExecutionException, InterruptedException {

        //定义证书文件路径
        File cerfile = new File("example-client.pfx");

        CerLoad cerLoad = new PKCS12LoadImp();    //加载pfx证书文件的实例
        //CerLoad cerLoad = new DerPemLoadImp();    加载der和pem证书文件的实例
        X509Certificate certificate  =cerLoad.getCer(cerfile);  //获取证书对象
        KeyPair keyPair = cerLoad.getKeyPair(cerfile);          //获取密钥对 对象

        //搜索OPC节点
        EndpointDescription[] endpointDescriptions = null;
        System.out.println("开始OPC连接...");
        try{
            endpointDescriptions = UaTcpStackClient.getEndpoints(EndPointUrl).get();
            System.err.println(endpointDescriptions[1]);
        }catch (Throwable e){
            System.out.println("获取端点失败");
            return;
        }

        // 选择相应的 desctiption,选择 匿名连接方式连接
        for(EndpointDescription ed:endpointDescriptions){
            if(ed.getSecurityLevel().equals("0")){

            }
        }
        EndpointDescription endpointDescription = endpointDescriptions[0];

        //创建OpcUaClientConfig 配置对象
        OpcUaClientConfig config = OpcUaClientConfig.builder()
                //.setApplicationName(LocalizedText.english("SimulationServer1"))
                //.setApplicationUri("urn:KOTIN:OPCUA:SimulationServer1")
                //.setCertificate(certificate)
                //.setKeyPair(keyPair)
                .setEndpoint(endpointDescription)
                //.setIdentityProvider(new UsernameProvider("cxcx","251128856"))
                .setRequestTimeout(uint(5000))
                .build();

        //利用OpcUaClientConfig创建OPC UA客户端
        OpcUaClient opcClient = new OpcUaClient(config);

        //opcClient.connect().isCancelled();  看看含义

        //获取连接
        opcClient.connect().get();

        //写入值
        int v = 99;

        NodeId nodeId = new NodeId(3,"Int32");
        Variant value = new Variant(v);
        DataValue dataValue = new DataValue(value,null,null);

        StatusCode statusCode = opcClient.writeValue(nodeId,dataValue).get();

        System.out.println(statusCode.isGood());


    }
}
